package com.ruoyi.system.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.utils.CommonStreamUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.WebTableDataInfo;
import com.ruoyi.system.domain.BaseBanner;
import com.ruoyi.system.domain.BaseContest;
import com.ruoyi.system.domain.bm.BMPack;
import com.ruoyi.system.domain.dto.Web.BannerDto;
import com.ruoyi.system.service.IBaseBMPackService;
import com.ruoyi.system.service.IBaseBannerService;
import com.ruoyi.system.service.IBaseContestService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理后台-礼包管理
 */
@ApiOperation("礼包管理")
@RestController
@RequestMapping("/web/pack")
public class BaseWebPackController extends BaseController {

    @Resource
    private IBaseBMPackService bmPackService;

    @Resource
    private IBaseContestService baseContestService;
    /**
     * 查询礼包管理列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询礼包管理列表", description = "查询礼包管理列表")
    public WebTableDataInfo list(BMPack dto)
    {
        List<BaseContest> baseContests = baseContestService.selectBaseContestList(new BaseContest());
        if (CollUtil.isEmpty(baseContests)){
            return getWebDataTable(new ArrayList<>());
        }
        dto.setContestIds(CommonStreamUtil.transList(baseContests, BaseContest::getId));
        startPage();
        List<BMPack> list = bmPackService.selectList(dto);
        return getWebDataTable(list);
    }

    /**
     * 新增礼包
     */
    @PostMapping("/save")
    @Operation(summary = "新增礼包", description = "新增礼包")
    public AjaxResult save(@RequestBody BMPack dto)
    {
        return toAjax(bmPackService.insert(dto));
    }

    /**
     * 编辑礼包
     */
    @PostMapping("/modify")
    @Operation(summary = "编辑礼包", description = "编辑礼包")
    public AjaxResult modify(@RequestBody BMPack dto)
    {
        return toAjax(bmPackService.modify(dto));
    }

    /**
     * 删除礼包
     */
    @PostMapping("/delete/{ids}")
    @Operation(summary = "删除礼包", description = "删除礼包")
    public AjaxResult delete(@PathVariable Long[] ids)
    {
        BMPack pack = new BMPack();
        pack.setIsDeleted(1);
        return toAjax(bmPackService.update(pack,
                Wrappers.<BMPack>lambdaQuery().in(BMPack::getId, ids)));
    }

}
